define(['angularAMD'], function (angularAMD) {
    angularAMD.service('B0232Service', ['$rootScope', '$filter', '$translate', 'config', '$timeout', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $timeout, $MMWService, $mdDialog){

            this.init = function (options) {
                var isEdit = false;
                if(!options){
                    options = {machine_no: '', maintain_order_list: []};
                }

                // 取得不良原因
                function loadBadReasonList(callback, cancelback){
                    $rootScope.showQueryList({
                        key:'page_no',
                        title: $filter('translate')('common.queryList.badReason_select'),
                        header : [
                            {label: $filter('translate')('common.queryList.reason_c'), code:'page_no'},
                            {label: $filter('translate')('common.queryList.reason_n'), code:'page_name'}
                        ],
                        query: function(conditions, feedback){
                            var content = {
                                page_mode: '8',
                                query_condition_info: [{column_name: 'REASONSUBTYPE', query_mode: '0', value: 'MAINTAIN', merge_condition_model: 'AND' }]
                            };

                            pageNumberInfoGet(content, function(data){
                                feedback(data);
                            });
                        },
                        confirm: function(selectItem, dialog){
                            dialog.hide();
                            if (callback)
                                callback(selectItem);
                        },
                        cancel:function(dialog){
                            dialog.hide();
                            if (typeof(cancelback) == 'function')
                                cancelback();
                        }
                    });
                }

                // 20220126 modify by Alan for#106824 : 修正查尋條件
                // 取得員工清單
                function loadEmployeeList(employee_no, callback){
                    $rootScope.showQueryList({
                        key: 'employee_no',
                        pageRecord:50,
                        condition:[
                            {name:$filter('translate')('common.queryList.employee_c'), column_name: 'a.USERNO', query_mode:'0', value: employee_no}
                        ],
                        header : [
                            {label: $filter('translate')('common.queryList.employee_c'), code:'employee_no'},
                            {label: $filter('translate')('common.queryList.employee_n'), code:'employee_name'}
                        ],
                        query: function(conditions, feedback){
                            var content = {
                                page_mode: 'user_query_no_process_report',
                                query_condition_info: conditions
                            };
                            pageNumberInfoGet(content, function(data){
                                feedback(data);
                            });
                        },
                        confirm: function(selectItem, dialog){
                            dialog.hide();
                            if (callback)
                                callback(selectItem);
                        }
                    });
                }

                //取得使用者資訊
                function pageNumberInfoGet(content, callback, errorback){
                    $MMWService.pageNumberInfoGet(content, function(itemList){
                        if(callback)
                            callback(itemList);
                    }, errorback);
                }

                // 20220901 modify by 王晴晴 for#0115102 : 修正栏位显示异常，删除“执行状况”栏位
                // 保養單資訊取得
                function maintainOrderInfoGet(callback){
                    var content = {
                        machine_no: options.machine_no
                    };
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_EQP.maintain_order_info_get',
                        content: content,
                        success: function (data) {
                            if (callback)
                                callback(data);
                        }
                    });
                }

                function maintainStatusUpdateProcessImp(maintain_order_list, callback){
                    var content = {
                        machine_no: options.machine_no,
                        maintain_order_list: []
                    };
                    if(maintain_order_list && maintain_order_list.length > 0){
                        maintain_order_list.forEach(function(item){
                            content.maintain_order_list.push({
                                wo_no: item.wo_no,
                                seq: item.seq,
                                finish_time: item.finish_time,
                                reason_no: item.reason_no,
                                labor_no: item.labor_no,
                                execute_status: item.execute_status
                            });
                        });
                        maintainStatusUpdateProcess(content, function(){
                            if (callback)
                                callback();
                        });
                    } else {
                        if (callback)
                            callback();
                    }
                }
                // 保養狀態更新作業
                function maintainStatusUpdateProcess(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_EQP.maintain_status_update_process',
                        content: content,
                        success: function (data) {
                            if (callback)
                                callback(data);
                        }
                    });
                }
                function beforeOpenDialog(callback){
                    if(options.machine_no != ''){
                        maintainOrderInfoGet(function(data){
                            if(data.maintain_order_list && data.maintain_order_list.length > 0){
                                if (callback)
                                    callback(data.maintain_order_list);
                            } else {
                                $rootScope.showAlert($filter('translate')('common.msg.no_data'));
                            }
                        });
                    }
                }

                function openDialog(data) {
                    $mdDialog.dialog(
                        'module/moduleB/program/B02/B0232/B0232.html',
                        function(dialog){
                            return {
                                css_url:'module/moduleB/program/B02/B0232/B0232.css',
                                maintain_order_list : data,
                                beforeShown: function(){
                                    if (dialog.maintain_order_list && dialog.maintain_order_list.length > 0){
                                        dialog.clickItem(dialog.maintain_order_list[0]); //預設勾選第一筆
                                    }
                                },
                                //20220802 modify by 王晴晴 for#0115102 : 点击按钮切换执行状态
                                clickChecked: function (item) {
                                    if (item.execute_status == null || item.execute_status == '0') {
                                        item.finish_time = moment().format('YYYY-MM-DD HH:mm:SS');
                                        item.execute_status = '1'
                                        if (item.execute_status = '1') {
                                            item.execute_status_name = get_execute_status(item.execute_status);
                                        }
                                    } else {
                                        item.finish_time = '';
                                        item.execute_status = '0'
                                        if (item.execute_status = '0') {
                                            item.execute_status_name = get_execute_status(item.execute_status);
                                        }
                                    }
                                    isEdit = true;
                                },
                                openESOP: function(item, event){
                                    event.stopPropagation();
                                    $mdDialog.serviceDialog('B0212Service', {
                                        esop_detail: item.esop_detail
                                    });
                                },
                                clickItem: function(item){
                                    dialog.maintainItem = item;
                                },
                                loadEmployeeList: function(){
                                    loadEmployeeList(dialog.maintainItem.labor_no, function(selectItem){
                                        dialog.maintainItem.labor_no = selectItem.employee_no;
                                        dialog.maintainItem.labor_name = selectItem.employee_name;
                                        isEdit = true;
                                    });
                                },
                                getLaborDisplay: function(item){
                                    var labor_display = '';
                                    if(item){
                                        if(item.labor_no && item.labor_no != ''){
                                            labor_display = item.labor_no;
                                        }
                                        if(item.labor_name && item.labor_name != ''){
                                            labor_display = labor_display + '(' +item.labor_name + ')';
                                        }
                                    }
                                    return labor_display;
                                },
                                loadBadReasonList: function(){
                                    loadBadReasonList(function(selectItem){
                                        dialog.maintainItem.reason_no = selectItem.page_no;
                                        dialog.maintainItem.reason_name = selectItem.page_name;
                                        isEdit = true;
                                    }, function(selectItem){
                                        dialog.maintainItem.reason_no = '';
                                        dialog.maintainItem.reason_name = '';
                                        isEdit = true;
                                    });
                                },
                                getReasonDisplay: function(item){
                                    var reason_display = '';
                                    if(item){
                                        if(item.reason_no && item.reason_no != ''){
                                            reason_display = item.reason_no;
                                        }
                                        if(item.reason_name && item.reason_name != ''){
                                            reason_display = reason_display + '(' +item.reason_name + ')';
                                        }
                                    }
                                    return reason_display;
                                },
                                getToastMsg: function(item){
                                    var wo_type = $filter('translate')('B0232.wo_type') + $filter('translate')('B0232.wo_type_' + item.wo_type);
                                    var release_date = $filter('translate')('B0232.release_date', {release_date: item.release_date});
                                    var description = $filter('translate')('B0232.description', {description: item.description});
                                    return wo_type + '<br>' + release_date + '<br>' + description;
                                },
                                back: function () {
                                    if(isEdit){
                                        $rootScope.showConfirm($filter('translate')('B0232.msg.has_dirty_value'), function(confirmDialog){
                                            confirmDialog.hide();
                                            dialog.hide();
                                            if (options.callback)
                                                options.callback();
                                        });
                                    }else{
                                        dialog.hide();
                                        if (options.callback)
                                            options.callback();
                                    }
                                },
                                confirm: function () {
                                    maintainStatusUpdateProcessImp(dialog.maintain_order_list, function(){
                                        dialog.hide();
                                        if (options.callback)
                                            options.callback();
                                    });
                                }
                            };
                        }
                    );
                }

                beforeOpenDialog(function(data){
                    openDialog(data);
                });
            };
        }]);
});
